package com.wxyzz.canteenadmin.order.orderList.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxyzz.canteenadmin.order.orderList.mapper.OrderMapper;
import com.wxyzz.canteenadmin.order.orderList.param.OrderParam;
import com.wxyzz.canteenadmin.order.orderList.service.OrderService;
import com.wxyzz.canteenadmin.order.orderList.vo.MemberInfoVo;
import com.wxyzz.canteenadmin.order.orderList.vo.OrderListVo;
import com.wxyzz.canteencommon.entity.ordermanager.Order;
import com.wxyzz.canteencommon.exception.CustmerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Override
    public PageInfo<OrderListVo> getOrder(OrderParam orderParam) {
        PageHelper.startPage(orderParam.getPageNum(),orderParam.getPageSize());
        List<OrderListVo> orders = orderMapper.selectOrder(orderParam.getOrderId(),
                orderParam.getMemberId(), orderParam.getStartTime(), orderParam.getEndTime(),
                orderParam.getOrderType(), orderParam.getBelongingStoreId(),
                orderParam.getPaymentStatus(), orderParam.getDistributionStatus());
        PageInfo<OrderListVo> orderPageInfo = new PageInfo<>(orders);
        return orderPageInfo;
    }

    @Override
    public OrderListVo getOneOrderById(Integer orderId) {
        OrderListVo orderListVo = orderMapper.selectOneOrder(orderId);
        return orderListVo;
    }

    @Override
    public void updateOrder(Order order) {
        int i = orderMapper.updateOrder(order);
        if (i == 0) {
            throw new CustmerException("更新失败");
        }
    }

    @Override
    public void deleteOrder(Integer orderId) {
        int i = orderMapper.deleteOrder(orderId);
        if (i == 0) {
            throw new CustmerException("删除失败");
        }
    }

    // 通过订单号查询会员信息
    @Override
    public MemberInfoVo getMemberInfoByOrderNumber(String orderNumber) {
        MemberInfoVo memberInfoVo = orderMapper.selectMemberInfoByOrderNumber(orderNumber);
        if (memberInfoVo == null) {
            throw new CustmerException("没有该会员信息");
        }
        return memberInfoVo;
    }

    // 查询某个数据通过订单号
    @Override
    public OrderListVo getOrderByOrderNumber(String orderNumber) {
        OrderListVo orderListVo = orderMapper.selectOrderByOrderNumber(orderNumber);
        if (orderListVo == null) {
            throw new CustmerException("查询失败");
        }
        return orderListVo;
    }
}
